package cn.gcyl.module.biz.service.usersubscriptionwordslog;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gcyl.module.biz.controller.admin.usersubscriptionwordslog.vo.*;
import cn.gcyl.module.biz.dal.dataobject.usersubscriptionwordslog.UserSubscriptionWordsLogDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.usersubscriptionwordslog.UserSubscriptionWordsLogMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.USER_SUBSCRIPTION_WORDS_LOG_NOT_EXISTS;

/**
 * 用户订阅词记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserSubscriptionWordsLogServiceImpl implements UserSubscriptionWordsLogService {

    @Resource
    private UserSubscriptionWordsLogMapper userSubscriptionWordsLogMapper;

    @Override
    public Long createUserSubscriptionWordsLog(UserSubscriptionWordsLogSaveReqVO createReqVO) {
        // 插入
        UserSubscriptionWordsLogDO userSubscriptionWordsLog = BeanUtils.toBean(createReqVO, UserSubscriptionWordsLogDO.class);
        Long loginUserId = getLoginUserId();
        if (ObjectUtil.isNotEmpty(loginUserId)){
            userSubscriptionWordsLog.setUserId(loginUserId);
        }
        userSubscriptionWordsLogMapper.insert(userSubscriptionWordsLog);

        // 返回
        return userSubscriptionWordsLog.getId();
    }

    @Override
    public void updateUserSubscriptionWordsLog(UserSubscriptionWordsLogSaveReqVO updateReqVO) {
        // 校验存在
        validateUserSubscriptionWordsLogExists(updateReqVO.getId());
        // 更新
        UserSubscriptionWordsLogDO updateObj = BeanUtils.toBean(updateReqVO, UserSubscriptionWordsLogDO.class);
        userSubscriptionWordsLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserSubscriptionWordsLog(Long id) {
        // 校验存在
        validateUserSubscriptionWordsLogExists(id);
        // 删除
        userSubscriptionWordsLogMapper.deleteById(id);
    }

    @Override
        public void deleteUserSubscriptionWordsLogListByIds(List<Long> ids) {
        // 删除
        userSubscriptionWordsLogMapper.deleteByIds(ids);
        }


    private void validateUserSubscriptionWordsLogExists(Long id) {
        if (userSubscriptionWordsLogMapper.selectById(id) == null) {
            throw exception(USER_SUBSCRIPTION_WORDS_LOG_NOT_EXISTS);
        }
    }

    @Override
    public UserSubscriptionWordsLogDO getUserSubscriptionWordsLog(Long id) {
        return userSubscriptionWordsLogMapper.selectById(id);
    }

    @Override
    public PageResult<UserSubscriptionWordsLogDO> getUserSubscriptionWordsLogPage(UserSubscriptionWordsLogPageReqVO pageReqVO) {
        return userSubscriptionWordsLogMapper.selectPage(pageReqVO);
    }

}